<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 200px;
        height: 3000px;
        background: violet;
        margin: 0 auto;
      }
    </style>
  </head>
  <body>
    <!-- 3.回到顶部功能（慢慢的一点点）setinterval来减 -->
    <div></div>
    <button>返回顶部</button>
    <script>
      var btnEle = document.querySelector("button");
      window.onscroll = function () {
        console.log(document.documentElement.scrollTop);
      };
      btnEle.onclick = function () {
        var num = document.documentElement.scrollTop;
        var timer = setInterval(function () {
          document.documentElement.scrollTop -= 1;
          if (document.documentElement.scrollTop <= 0) clearInterval(timer);
        }, 10);
      };
    </script>
  </body>
</html>
